package com.um.jdy.app.shop.manager;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.shop.entity.vo.GoodsCategoryGoodsVO;
import com.um.jdy.app.shop.entity.vo.GoodsCategoryTreeNodeVO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.shop.entity.po.Goods;
import com.um.jdy.common.shop.entity.po.GoodsCategory;
import com.um.jdy.common.shop.entity.vo.GoodsVO;
import com.um.jdy.common.shop.service.GoodsCategoryService;
import com.um.jdy.common.shop.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class GoodsCategoryManager {

    @Autowired
    GoodsCategoryService goodsCategoryService;

    @Autowired
    GoodsService goodsService;

    /**
     * 查询商品类目树
     * @return
     */
    public List<GoodsCategoryTreeNodeVO> getTree(){
        List<GoodsCategory> goodsCategoryServiceList = goodsCategoryService.getList(new String[]{}, new String[]{},
                new String[]{"sort_no"}, new String[]{"asc"});
        List<GoodsCategoryTreeNodeVO> treeNodeVOList = BeanUtil.copyToList(goodsCategoryServiceList, GoodsCategoryTreeNodeVO.class);

        List<GoodsCategoryTreeNodeVO> parentList = treeNodeVOList.stream()
                .filter(item -> item.getShopGoodsCategoryParentId().equals("0")).collect(Collectors.toList());

        parentList.stream().forEach(item -> item.setChildren(treeNodeVOList.stream()
                .filter(treeNode -> treeNode.getShopGoodsCategoryParentId().equals(item.getShopGoodsCategoryId()))
                .collect(Collectors.toList())));

        return parentList;
    }

    public List<GoodsCategoryGoodsVO> getCategoryAndGoods(){
        List<GoodsCategory> categoryList = goodsCategoryService.getList(
                new String[]{"shop_goods_category_parent_id"}, new String[]{"0"},
                new String[]{"sort_no"}, new String[]{"asc"});
        List<GoodsCategoryGoodsVO> categoryGoodsList = BeanUtil.copyToList(categoryList, GoodsCategoryGoodsVO.class);

        List<Goods> goodsList = goodsService.getList(new String[]{"is_upper"}, new String[]{YesOrNo.Yes.name()},
                new String[]{"create_date"}, new String[]{"desc"});
        List<GoodsVO> goodsVOList = BeanUtil.copyToList(goodsList, GoodsVO.class);

        categoryGoodsList.stream().forEach(item -> item.setGoodsList(goodsVOList.stream()
                .filter(treeNode -> StrUtil.isNotBlank(item.getCode()) && item.getCode().equals(treeNode.getBigCode()))
                .collect(Collectors.toList())));

        return categoryGoodsList;
    }


}
